package controllers

import (
	"beego/redis"
	"beego/tool"
	"github.com/astaxie/beego"
	"time"
)

type NbaGameController struct {
	beego.Controller
}

func (c *NbaGameController) Get() {
	redis.Init()
	id := c.Ctx.Input.Param(":id")
	gameCache, _ := redis.GetRedis(id)
	if gameCache == nil {
		game := tool.SendGetRQ("https://china.nba.cn/stats2/game/playbyplay.json?gameId=" + id)
		c.Data["json"] = tool.UnmarshalJson(game)
		c.ServeJSON()
		redis.SetRedis(game, id, TimeOfNba())
	} else {
		c.Data["json"] = tool.UnmarshalJson(gameCache)
		c.ServeJSON()
	}
}
func TimeOfNba() int {
	if time.Now().Hour() > 14 || time.Now().Hour() < 8 {
		return 1800
	} else {
		return 10
	}
}
